package com.fruit.controller.app.index;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fruit.constants.GoodsConstant;
import com.fruit.controller.base.BaseController;
import com.fruit.pojo.common.Page;
import com.fruit.service.index.IndexManager;
import com.fruit.util.DateUtil;
import com.fruit.util.ResultInfo;
/**
 * APP首页controller
 * 
 * @author shibeilei
 *
 */
@Controller
@RequestMapping(value = "/app/")
public class IndexController extends BaseController {
	
	@Resource(name="indexService")
	private IndexManager indexService;

	/**
	 * 首页
	 * @param page
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/index", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> index(Page page,HttpServletRequest request,HttpSession session) {
		this.logBefore(logger, "APP首页-开始");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			// 首页导航推荐位
			Map<String, Object> postionMap = new HashMap<String, Object>();
			postionMap.put("position", GoodsConstant.APPINDEXBANNER);
			List<Map<String, Object>> postionList = indexService.getRecommended(postionMap);//获取首页推荐位
			resultMap.put("indexBannerList",postionList);
			//当季时令推荐位
			Map<String, Object> seasonalMap = new HashMap<String, Object>();
			seasonalMap.put("start", 0);
			seasonalMap.put("num", 6);
			seasonalMap.put("position", GoodsConstant.SEASONAL);
			seasonalMap.put("curtime", DateUtil.getTime());
			List<Map<String, Object>> seasonalList = indexService.getGoodsListByPosition(seasonalMap);//当季时令推荐位
			resultMap.put("seasonalList",seasonalList);
			//热带水果
			Integer classId = indexService.getIdByCode("popular");//查询热带水果分类的id
			if(classId != null){
				List<Map<String, Object>> tropicList = indexService.getNameById(classId);//根据热带水果分类id查询所有的水果名称
				resultMap.put("tropicList",tropicList);
			}
			//获取商品列表
			Map<String, Object> goodsMap = new HashMap<String, Object>();
			goodsMap.put("start", (page.getPage()-1) * page.getShowNumber());
			goodsMap.put("num", page.getShowNumber());
			goodsMap.put("curtime", DateUtil.getTime());
			List<Map<String, Object>> goodsList = indexService.getGoodsList(goodsMap);//获取商品列表
			if(goodsList!=null){
				for(int i=0;i<goodsList.size();i++){
					Map<String, Object> goodsMaps = goodsList.get(i);
					String goodsNames = goodsMaps.get("levels") + "-" + goodsMaps.get("goodsName");
					goodsMaps.put("goodsName", goodsNames);
				}
			}
			resultMap.put("goodsList",goodsList);
			resultMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resultMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resultMap;
	}

	/**
	 * 首页商品 加载更多
	 * @param page
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/getGoods", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> getGoods(Page page,HttpServletRequest request,HttpSession session) {
		this.logBefore(logger, "APP首页商品加载更多-开始");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			//获取商品列表
			Map<String, Object> goodsMap = new HashMap<String, Object>();
			goodsMap.put("start", (page.getPage()-1) * page.getShowNumber());
			goodsMap.put("num", page.getShowNumber());
			goodsMap.put("curtime", DateUtil.getTime());
			List<Map<String, Object>> goodsList = indexService.getGoodsList(goodsMap);//获取商品列表
			if(goodsList!=null){
				for(int i=0;i<goodsList.size();i++){
					Map<String, Object> goodsMaps = goodsList.get(i);
					String goodsNames = goodsMaps.get("levels") + "-" + goodsMaps.get("goodsName");
					goodsMaps.put("goodsName", goodsNames);
				}
			}
			resultMap.put("goodsList",goodsList);
			resultMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resultMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resultMap;
	}
	
	/**
	 * 搜索
	 * @param page
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/search", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> search(Page page,HttpServletRequest request,HttpSession session) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			//根据关键字或者商品名称搜索
			Map<String, Object> goodsMap = new HashMap<String, Object>();
			goodsMap.put("start", (page.getPage()-1) * page.getShowNumber());
			goodsMap.put("num", page.getShowNumber());
			goodsMap.put("key", request.getParameter("keyword"));
			goodsMap.put("curtime", DateUtil.getTime());
			List<Map<String, Object>> goodsList = indexService.getListByNameAndKeyword(goodsMap);//获取搜索的结果
			if(goodsList!=null){
				for(int i=0;i<goodsList.size();i++){
					Map<String, Object> goodsMaps = goodsList.get(i);
					String goodsNames = goodsMaps.get("levels") + "-" + goodsMaps.get("goodsName");
					goodsMaps.put("goodsName", goodsNames);
				}
			}
			resultMap.put("goodsList",goodsList);
			resultMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			resultMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		return resultMap;
	}
	
	/**
	 * 热门标签
	 * @param page
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/searchLable", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> searchLable(Page page,HttpServletRequest request,HttpSession session) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			//根据关键字或者商品名称搜索
			Map<String, Object> goodsMap = new HashMap<String, Object>();
			goodsMap.put("start", 0);
			goodsMap.put("num", 8);
			resultMap.put("searchLableList",indexService.searchLable(goodsMap));//获取搜索的结果
			resultMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			resultMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		return resultMap;
	}
	
	/**
	 * 热带水果列表（根据某一分类查询出来这一分类下的所有水果）
	 */
	@RequestMapping(value = "/tropicFruit", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> tropicFruit(Page page,Integer id,HttpServletRequest request,HttpSession session) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			//根据某一分类查询出来这一分类下的所有水果
			Map<String, Object> goodsMap = new HashMap<String, Object>();
			goodsMap.put("start", (page.getPage()-1) * page.getShowNumber());
			goodsMap.put("num", page.getShowNumber());
			goodsMap.put("id", id);
			goodsMap.put("curtime", DateUtil.getTime());
			List<Map<String, Object>> goodsList = indexService.getNameByClassId(goodsMap);//获取搜索的结果
			if(goodsList!=null){
				for(int i=0;i<goodsList.size();i++){
					Map<String, Object> goodsMaps = goodsList.get(i);
					String goodsNames = goodsMaps.get("levels") + "-" + goodsMaps.get("goodsName");
					goodsMaps.put("goodsName", goodsNames);
				}
			}
			resultMap.put("goodsList",goodsList);
			resultMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			resultMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		return resultMap;
	}
}
